package com.polidea.rxohosble2.scan;


import com.polidea.rxohosble2.RxBleDevice;
import ohos.bluetooth.ble.BleScanResult;
import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

public class ScanResult implements Sequenceable {

    private final RxBleDevice bleDevice;
    private String macAddress;
    private String name;
    private int rssi;
    private long timestampNanos;
    private final ScanCallbackType callbackType;
    private final ScanRecord scanRecord;

    public ScanResult(RxBleDevice bleDevice, int rssi, long timestampNanos, ScanCallbackType callbackType, ScanRecord scanRecord) {
        this.bleDevice = bleDevice;
        this.rssi = rssi;
        this.timestampNanos = timestampNanos;
        this.callbackType = callbackType;
        this.scanRecord = scanRecord;
    }

    public ScanResult(String macAddress) {
        this.bleDevice = null;
        this.macAddress = macAddress;
        this.callbackType = ScanCallbackType.CALLBACK_TYPE_UNKNOWN;
        this.scanRecord = null;
    }

    public ScanResult(BleScanResult bleScanResult) {
        this.bleDevice = null;
        this.macAddress = bleScanResult.getPeripheralDevice().getDeviceAddr();
        this.name = bleScanResult.getPeripheralDevice().getDeviceName().orElse("");
        this.rssi = bleScanResult.getRssi();
        this.timestampNanos = bleScanResult.getTime();
        this.callbackType = ScanCallbackType.CALLBACK_TYPE_UNKNOWN;
        this.scanRecord = null;
    }

    public RxBleDevice getBleDevice() {
        return bleDevice;
    }

    public int getRssi() {
        return rssi;
    }

    public long getTimestampNanos() {
        return timestampNanos;
    }

    public ScanCallbackType getCallbackType() {
        return callbackType;
    }

    public ScanRecord getScanRecord() {
        return scanRecord;
    }

    public String getMacAddress() {
        return macAddress;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "ScanResult{" +
                "bleDevice=" + bleDevice +
                ", macAddress='" + macAddress + '\'' +
                ", name='" + name + '\'' +
                ", rssi=" + rssi +
                ", timestampNanos=" + timestampNanos +
                ", callbackType=" + callbackType +
                ", scanRecord=" + scanRecord +
                '}';
    }

    @Override
    public boolean marshalling(Parcel parcel) {
        parcel.writeString(macAddress);
        parcel.writeString(name);
        parcel.writeInt(rssi);
        parcel.writeLong(timestampNanos);
        return true;
    }

    @Override
    public boolean unmarshalling(Parcel parcel) {
        this.macAddress = parcel.readString();
        this.name = parcel.readString();
        this.rssi = parcel.readInt();
        this.timestampNanos = parcel.readLong();
        return true;
    }
}
